package cn.fintecher.gis.service;

import cn.fintecher.gis.entity.LandQuo;
import cn.fintecher.gis.enums.FlowType;
import cn.fintecher.gis.enums.Rapporttype;
import cn.fintecher.gis.exception.BadRequestException;
import cn.fintecher.gis.model.MapSpotData;
import cn.fintecher.gis.model.ParsingShpResults;
import cn.fintecher.gis.model.response.FlowBusinessResponse;
import cn.fintecher.gis.postgis.PostGISService;
import org.apache.commons.lang3.StringUtils;
import org.geotools.filter.text.cql2.CQLException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service("")
public class DetectionService extends BaseService {

    private final Logger logger = LoggerFactory.getLogger(DetectionService.class);

    @Autowired
    private PostGISService postGISService;

    @Autowired
    private LandQuoService landQuoService;

    @Autowired
    private LayerStatisticalService layerStatisticalService;

    @Autowired
    private FlowService flowService;

    /**
     * 根据code获取对应的名称
     *
     * @param code
     * @param rapporttype
     * @return
     */
    public String getLandQuoName(String code, Rapporttype rapporttype) {
        List<LandQuo> landQuoList = new ArrayList<>();
        if (StringUtils.isNotEmpty(code)) {
            landQuoList = landQuoService.getLandQuoByCode(code, rapporttype);
        }
        String name = new String();
        if (landQuoList != null && !landQuoList.isEmpty()) {
            name = landQuoList.get(0).getName();
        } else {
            name = "未知";
        }
        return name;
    }

    /**
     * 区域和图层作对比
     *
     * @param wkt
     * @param layerCode
     * @param cql
     * @return
     * @throws Exception
     */
    public List<MapSpotData> getDetectionMap(String wkt, String layerCode, String cql) throws Exception {
        ParsingShpResults result = postGISService.intersectionWkt(wkt, layerCode, cql);
        if (!result.getIsSuccess()) {
            throw new BadRequestException("importShp", result.getMessage());
        }
        String tableName = result.getMessage();
        List<MapSpotData> mapSpotData = postGISService.queryMapSpot("", tableName);
        postGISService.removeSchema(tableName);
        return mapSpotData;
    }


    /**
     * 图层之间的对比
     *
     * @param layerCode1
     * @param layerCode2
     * @param cql
     * @return
     * @throws IOException
     * @throws CQLException
     */
    public List<MapSpotData> getDetectionLayerCode(String layerCode1, String layerCode2, String cql) throws Exception {
        ParsingShpResults result = postGISService.intersectionLayer(layerCode1, layerCode2, cql);
        if (!result.getIsSuccess()) {
            throw new BadRequestException("intersectionLayerGeoJson", "对比图层出错");
        }
        String tableName = result.getMessage();
        List<MapSpotData> mapSpotData = postGISService.queryMapSpot("", tableName);
        postGISService.removeSchema(tableName);
        return mapSpotData;
    }


    /**
     * 业务分析（图层对比）
     *
     * @param flowType
     * @param layerCode
     * @return
     * @throws Exception
     */
    public List<FlowBusinessResponse> getBusinessLayerCode(FlowType flowType, String layerCode) throws Exception {
        List<FlowBusinessResponse> results = new ArrayList<>();
        List<FlowBusinessResponse> list = flowService.getFinishList(flowType);
        for (FlowBusinessResponse flowBusiness : list) {
            if (StringUtils.isNotEmpty(flowBusiness.getLayerCode())) {
                List<MapSpotData> mapList = getDetectionLayerCode(layerCode, flowBusiness.getLayerCode(), null);
                if (mapList != null && mapList.size() > 0) {
                    results.add(flowBusiness);
                }
            }
        }
        return results;
    }


    /**
     * 业务分析（区域对比）
     *
     * @param flowType
     * @param wkt
     * @return
     * @throws Exception
     */
    public List<FlowBusinessResponse> getBusinessWkt(FlowType flowType, String wkt) throws Exception {
        List<FlowBusinessResponse> results = new ArrayList<>();
        List<FlowBusinessResponse> list = flowService.getFinishList(flowType);
        for (FlowBusinessResponse flowBusiness : list) {
            if (StringUtils.isNotEmpty(flowBusiness.getLayerCode())) {
                List<MapSpotData> dataList = getDetectionMap(wkt, flowBusiness.getLayerCode(), null);
                if (dataList != null && dataList.size() > 0) {
                    results.add(flowBusiness);
                }
            }
        }
        return results;
    }
}
